$(document).ready(function(){

  datePickerController.createDatePicker({
    formElements:{
      "event_begin_date_1i":"Y",
      "event_begin_date_2i":"n",
      "event_begin_date_3i":"j"
    }
  });

  datePickerController.createDatePicker({
    formElements:{
      // "inp1" represents the day part
      "event_end_date_1i":"Y",
      // "inp2" represents the month part 
      "event_end_date_2i":"n",
      // "inp3" represents the year part
      "event_end_date_3i":"j"
    }
  });

  if($('select#event_duration_type').val() != 4) {
    $('#end_date').hide();
    $('#begin_date label').text("Date:");
  }
  
  $('select#event_duration_type').change(function() {
    if ($('select#event_duration_type').val() == 4) {
      $('#end_date').fadeIn();
      $('#begin_date label').text("Begin Date:");
      $('#begin_date label').fadeIn(1500);
    } else {
      $('#end_date').fadeOut();
      $('#begin_date label').text("Date:");
    }
  });

  $('select#event_event_type').change(function() {
    if ($('select#event_event_type').val() > 4) {
      $('select#event_duration_type').val(4);
      $('#end_date').fadeIn();

      $('input#event_name').parent().fadeOut();

      $('#begin_date label').text("Begin Date:");
      $('#begin_date label').fadeIn(1500);
      $('select#event_end_date_3i').val(parseInt($('select#event_end_date_3i').val()) + 1)
      if ($('select#event_event_type').val() == 5) {
        $('input#event_name').val("Primary");
      } else if ($('select#event_event_type').val() == 6) {
        $('input#event_name').val("Secondary");
      }
    } else if (!$('input#event_name').parent().is("visible")) {
      if ($('input#event_name').val() == "Primary" ||
          $('input#event_name').val() == "Secondary") { 
        $('input#event_name').val("");
      }
      $('input#event_name').parent().fadeIn();
    }
  });
});
